package com.saifan.wyy_ov.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class JsonMapUtil {

	/**
	 * 把JSONArray解析成键值对形式
	 * 
	 * @param strJson
	 * @return
	 * @throws JSONException
	 */
	@SuppressWarnings("unchecked")
	public static List<Map<String, String>> getListMapByJsonString(String strJson) throws JSONException {
		List<Map<String, String>> lm = new ArrayList<Map<String, String>>();

		JSONArray ja = new JSONArray(strJson);
		for (int i = 0; i < ja.length(); i++) {
			JSONObject jo = ja.getJSONObject(i);
			Map<String, String> map = new HashMap<String, String>();
			Iterator<String> it = jo.keys();
			while (it.hasNext()) {
				String key = it.next();
				map.put(key, jo.get(key).toString());
			}
			lm.add(map);
		}
		return lm;

	}

	/**
	 * 把JSONObject解析成键值对形式
	 * @param strJson
	 * @return 键值对
	 * @throws JSONException
	 */
	public static Map<String, String> getMapByJsonString(String strJson) throws JSONException {
		JSONObject jo = new JSONObject(strJson);
		Map<String, String> map = new HashMap<String, String>();
		@SuppressWarnings("unchecked")
		Iterator<String> it = jo.keys();
		while (it.hasNext()) {
			String key = it.next();
			map.put(key, jo.get(key).toString());
		}
		return map;

	}
}
